(function() {
	$('#time-format').on('click', function() {
		$('.mask').css({
			display: 'block'
		});
		$('#timePicker').css({
			display: 'block'
		});
		//		$.post("http://192.168.0.37:8080/busgo/returnBusCarTime",function(data){
		//			console.log(1);
		//		});
		TimeConfirm('08:00', '16:00', 120);
	});

	function TimeConfirm(startTime, endTime, leadTime) {
		//将参数转变为周几
		function getWeek(week) {
			switch (week) {
				case 0:
					return '周日';
					break;
				case 1:
					return '周一';
					break;
				case 2:
					return '周二';
					break;
				case 3:
					return '周三';
					break;
				case 4:
					return '周四';
					break;
				case 5:
					return '周五';
					break;
				case 6:
					return '周六';
					break;
			}
		}
		//参数值小于10在前面添加0
		function addZero(value) {
			if (value < 10) {
				return '0' + value;
			} else {
				return value;
			}
		}
		//获取指定月份间隔
		function monthScope(value1, month) {
			var getMonth = function(str) {
				var list = str.split('-');
				var year = list[0];
				var month = parseInt(list[1]) - 1;
				var tempDate = new Date(year, month, 1);
				return tempDate;
			};
			var month1 = getMonth(value1);

			var nextMonth = new Date(month1.getFullYear(), month1.getMonth(), 1);
			nextMonth.setMonth(month1.getMonth() + month);

			var month2 = nextMonth;
			var monthArr = [];
			var i = 0;
			while (!(month1.getFullYear() == month2.getFullYear() && month1.getMonth() == month2.getMonth())) {
				monthArr[i] = month1.getFullYear() + '-' + (month1.getMonth() + 1);
				// console.log(monthArr[i])
				i++;
				month1.setMonth(month1.getMonth() + 1);
			}
			return monthArr;
		}

		function addMonth() {
			var monthStr = '';
			for (var i = 0; i < monthList.length; i++) {
				monthStr += '<li>' + monthList[i].split('-')[1] + '月</li>';
			}
			$('#pickMonth ul').html(monthStr);
		}

		//获取从当前日期到指定间隔几个月的所有日期
		function currentDateScope(value1) {
			//2018-2-1
			var list = value1.split('-');
			var currentDate = new Date(list[0], list[1] - 1, list[2]);

			var endDate = new Date();
			endDate.setFullYear(currentDate.getFullYear());
			endDate.setDate(1);
			endDate.setMonth(currentDate.getMonth() + 1);

			var currentDateArr = [];
			var i = 0;
			while (
				!(
					currentDate.getFullYear() == endDate.getFullYear() &&
					currentDate.getMonth() == endDate.getMonth() &&
					currentDate.getDate() == endDate.getDate()
				)
			) {
				currentDateArr[i] =
					currentDate.getFullYear() +
					'-' +
					(currentDate.getMonth() + 1) +
					'-' +
					currentDate.getDate() +
					'-' +
					currentDate.getDay();
				i++;
				currentDate.setDate(currentDate.getDate() + 1);
			}
			return currentDateArr;
		}

		function addDate() {
			var dateStr = '';
			for (var i = 0; i < dateList.length; i++) {
				var list = dateList[i].split('-');
				dateStr += '<li>' + list[2] + '日' + getWeek(parseInt(list[3])) + '</li>';
			}
			$('#pickDate ul').html(dateStr);
		}

		//获取指定间隔时间的所有时间
		function timeScope(startTime, endTime) {
			var getTime = function(str) {
				var tempTime = new Date();
				var list = str.split(':');
				tempTime.setHours(list[0]);
				tempTime.setMinutes(list[1]);
				return tempTime;
			};
			var time1 = getTime(startTime);
			var time2 = getTime(endTime);
			if (time1 > time2) {
				var tempDate = time1;
				time1 = time2;
				time2 = tempDate;
			}
			var timeArr = [];
			var i = 0;
			while (!(time1.getHours() == time2.getHours() && time1.getMinutes() == time2.getMinutes())) {
				var hourStr = time1.getHours().toString();
				var minuteStr = time1.getMinutes().toString();
				if (hourStr.length == 1) {
					hourStr = '0' + hourStr;
				}
				if (minuteStr.length == 1) {
					minuteStr = '0' + minuteStr;
				}
				timeArr[i] = hourStr + ':' + minuteStr;
				i++;
				time1.setMinutes(time1.getMinutes() + 30);
			}
			timeArr.push(endTime);
			return timeArr;
		}

		function addTime() {
			var timeStr = '';
			for (var i = 0; i < timeList.length; i++) {
				timeStr += '<li>' + timeList[i] + '</li>';
			}
			$('#pickTime ul').html(timeStr);
		}

		//获取当前时间
		var myDate = new Date();
		var currentYear = myDate.getFullYear(); //年份
		var currentMonth = myDate.getMonth(); //月份
		var currentDate = myDate.getDate(); //日期
		var currentWeek = myDate.getDay(); //周几
		var currentHour = myDate.getHours(); //小时
		var currentMinute = myDate.getMinutes(); //分钟

		// $('#time-format .Rental_times>.timer').text(timeStr)

		//对当前时间以半小时为间隔向上取整
		if (currentMinute <= 30) {
			currentMinute = 30;
		} else {
			currentHour = currentHour + 1;
			currentMinute = 0;
		}
		//获取取整后的当前时间
		var currentTime = currentHour + ':' + currentMinute;

		//获取起始时间，截止时间
		//暂定起始时间为8:00，截止时间为16:00
		var startTime = startTime; //起始时间
		var endTime = endTime; //截止时间
		var leadTime = leadTime; //提前时间（分钟）
		var dayEndTime = '23:30';

		var setTime = function(str) {
			var tempTime = new Date();
			var list = str.split(':');
			tempTime.setHours(list[0]);
			tempTime.setMinutes(list[1]);
			return tempTime;
		};
		var start = setTime(startTime);
		var current = setTime(currentTime);
		var end = setTime(endTime);

		var timeList = [];
		var frTimeList = [];
		var dateList = [];
		var frDateList = [];
		var monthList = [];
		if (current <= start) {
			monthList = monthScope(currentYear + '-' + (currentMonth + 1), 2);
			dateList = currentDateScope(currentYear + '-' + (currentMonth + 1) + '-' + currentDate);
			frDateList = currentDateScope(currentYear + '-' + (currentMonth + 1) + '-' + currentDate);
			start.setMinutes(start.getMinutes() + leadTime);
			var startStr = start.getHours() + ':' + start.getMinutes();
			timeList = timeScope(startStr, dayEndTime);
			frTimeList = timeScope(startStr, dayEndTime);
		} else if (current > start && current <= end) {
			current.setMinutes(current.getMinutes() + leadTime);
			var startStr = current.getHours() + ':' + current.getMinutes();
			monthList = monthScope(currentYear + '-' + (currentMonth + 1), 2);
			dateList = currentDateScope(currentYear + '-' + (currentMonth + 1) + '-' + currentDate);
			frDateList = currentDateScope(currentYear + '-' + (currentMonth + 1) + '-' + currentDate);
			timeList = timeScope(startStr, dayEndTime);
			frTimeList = timeScope(startStr, dayEndTime);
		} else {
			current.setDate(current.getDate() + 1);
			var dateStr = current.getFullYear() + '-' + (current.getMonth() + 1) + '-' + current.getDate();
			var monthStr = current.getFullYear() + '-' + (current.getMonth() + 1);
			start.setMinutes(start.getMinutes() + leadTime);
			var startStr = start.getHours() + ':' + start.getMinutes();
			dateList = currentDateScope(dateStr);
			frDateList = currentDateScope(dateStr);
			monthList = monthScope(monthStr, 2);
			timeList = timeScope(startStr, dayEndTime);
			frTimeList = timeScope(startStr, dayEndTime);
		}

		addTime();
		$('#pickTime ul li').eq(0).addClass('selected');

		addMonth();
		$('#pickMonth ul li').eq(0).addClass('selected');

		addDate();
		$('#pickDate ul li').eq(0).addClass('selected');

		//滚动变化部分逻辑

		document.addEventListener(
			'touchmove',
			function(e) {
				e.preventDefault();
			},
			isPassive()
				? {
						capture: false,
						passive: false
					}
				: false
		);

		var myScrollMonth = new IScroll('#pickMonth', {
			probeType: 3,
			deceleration: 0.001,
			bounce: false
		});
		var myScrollDate = new IScroll('#pickDate', {
			probeType: 3,
			deceleration: 0.001,
			bounce: false
		});
		var myScrollTime = new IScroll('#pickTime', {
			probeType: 3,
			deceleration: 0.001,
			bounce: false
		});

		myScrollMonth.on('scroll', function() {
			var fontSize = parseInt(document.documentElement.style.fontSize);
			var y = Math.round(this.y / fontSize / 1);
			$('#pickMonth ul li').eq(Math.abs(y)).addClass('selected').siblings().removeClass('selected');
		});
		myScrollMonth.on('scrollEnd', function() {
			var fontSize = parseInt(document.documentElement.style.fontSize);
			var y = Math.round(this.y / fontSize / 1);
			$('#pickMonth ul').css({
				transform: 'translate(0,' + 1 * y + 'rem)',
				transition: '0.1s all'
			});
			if (Math.abs(y) != 0) {
				timeList = timeScope('00:00', '23:30');
				addTime();
				$('#pickTime ul').css({
					transform: 'translate(0,0)',
					transition: '0.1s all'
				});
				$('#pickTime ul li').eq(0).addClass('selected');
				myScrollTime.refresh();

				dateList = currentDateScope(monthList[Math.abs(y)] + '-' + 1);
				addDate();
				$('#pickDate ul').css({
					transform: 'translate(0,0)',
					transition: '0.1s all'
				});
				$('#pickDate ul li').eq(0).addClass('selected');
				myScrollDate.refresh();
			} else {
				timeList = [];
				timeList = frTimeList;
				addTime();
				$('#pickTime ul').css({
					transform: 'translate(0,0)',
					transition: '0.1s all'
				});
				$('#pickTime ul li').eq(0).addClass('selected');
				myScrollTime.refresh();
				dateList = [];
				dateList = frDateList;
				addDate();
				$('#pickDate ul').css({
					transform: 'translate(0,0)',
					transition: '0.1s all'
				});
				$('#pickDate ul li').eq(0).addClass('selected');
				myScrollDate.refresh();
			}
		});

		myScrollDate.on('scroll', function() {
			var fontSize = parseInt(document.documentElement.style.fontSize);
			var y = Math.round(this.y / fontSize / 1);
			$('#pickDate ul li').eq(Math.abs(y)).addClass('selected').siblings().removeClass('selected');
		});
		myScrollDate.on('scrollEnd', function() {
			var fontSize = parseInt(document.documentElement.style.fontSize);
			var y = Math.round(this.y / fontSize / 1);
			$('#pickDate ul').css({
				transform: 'translate(0,' + 1 * y + 'rem)',
				transition: '0.1s all'
			});
			if (Math.abs(y) != 0) {
				timeList = timeScope('00:00', '23:30');
				addTime();
				$('#pickTime ul').css({
					transform: 'translate(0,0)',
					transition: '0.1s all'
				});
				$('#pickTime ul li').eq(0).addClass('selected');
				myScrollTime.refresh();
			} else {
				timeList = [];
				timeList = frTimeList;
				addTime();
				$('#pickTime ul').css({
					transform: 'translate(0,0)',
					transition: '0.1s all'
				});
				$('#pickTime ul li').eq(0).addClass('selected');
				myScrollTime.refresh();
			}
		});

		myScrollTime.on('scroll', function() {
			var fontSize = parseInt(document.documentElement.style.fontSize);
			var y = Math.round(this.y / fontSize / 1);

			$('#pickTime ul li').eq(Math.abs(y)).addClass('selected').siblings().removeClass('selected');
		});
		myScrollTime.on('scrollEnd', function() {
			var fontSize = parseInt(document.documentElement.style.fontSize);
			var y = Math.round(this.y / fontSize / 1);
			if (Math.abs(y) < $('#pickTime ul li').length) {
				$('#pickTime ul').css({
					transform: 'translate(0,' + 1 * y + 'rem)',
					transition: '0.1s all'
				});
			}
		});

		$('#timePicker .confirm').on('click', function() {
			var dateStr = dateList[$('#pickDate ul li.selected').index()];
			var timeStr = timeList[$('#pickTime ul li.selected').index()];
			var arr = dateStr.split('-');
			var selDate = addZero(arr[0]) + '-' + addZero(arr[1]) + '-' + addZero(arr[2]) + ' ' + timeStr;
			sessionStorage.setItem('starttime', selDate);

			var dateDay = addZero(arr[1]) + '月' + addZero(arr[2]) + '日';
			$('#time-format .Rental_date').text(dateDay);
			$('#time-format .Rental_times>.timer').text(timeStr);

			$('#timePicker').css({
				display: 'none'
			});
			$('.mask').css({
				display: 'none'
			});
		});
		$('#timePicker .cancel').on('click', function() {
			$('#timePicker').css({
				display: 'none'
			});
			$('.mask').css({
				display: 'none'
			});
		});
		$('.mask').on('click', function() {
			$('#timePicker').css({
				display: 'none'
			});
			$('.mask').css({
				display: 'none'
			});
		});
		//   function time() {
		//     var Daydatas = sessionStorage.getItem('Daydatas')
		//     sDate1 = startTime;
		//     sDate2=  Daydatas  ;
		//     console.log(sDate1, sDate2)
		//     var Days = DateDiff(sDate1, sDate2);
		//     $(".Content_module ul li.times .mid span em").text(Days)
		//   }
	}
	//     //计算天数差的函数，通用
	//     function DateDiff(sDate1, sDate2) {    //sDate1和sDate2是xxxx-xx-xx格式
	//       var aDate, oDate1, oDate2, iDays
	//       aDate = sDate1.split("-")
	//       oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])    //转换为xx-xx-xxxx格式
	//       aDate = sDate2.split("-")
	//       oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
	//       iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24)    //把相差的毫秒数转换为天数
	//       return iDays
	//     }
})();
